﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.esriSystem;

namespace ISchool
{
    class IOverlay
    {
        //作者：向桂泽
        //描述：空间叠置分析,包括叠置求并，叠置求交，叠置标识，叠置擦除，叠置相交取反，叠置更新
        //地图文档容器
        private AxMapControl mAxMapControls;
        //图层数目
        private int LayerCount;
        public string strOutputPath;
        public string strInputPath;
        public overLayType mOverLayType;
        private  ILayer[] mILayer;
        public enum overLayType
        {
            //叠置求并，叠置求交，叠置标识，叠置擦除，叠置相交取反，叠置更新
            Intersect,Union,Identity,Erase,SymmetricalDifference,Update
        }
        public IHookHelper mHookHelper = new HookHelperClass();
        public IOverlay(object hook)
        {
            this.mHookHelper.Hook = hook;
        }
        private IEnumLayer GetLayers()
        {
            //{6CA416B1-E160-11D2-9F4E-00C04F6BC78E} IDataLayer
            //{40A9E885-5533-11d0-98BE-00805F7CED21} IFeatureLayer
            //{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} IGeoFeatureLayer
            //{34B2EF81-F4AC-11D1-A245-080009B6F22B} IGraphicsLayer
            //{5CEAE408-4C0A-437F-9DB3-054D83919850} IFDOGraphicsLayer
            //{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E} ICoverageAnnotationLayer
            //{EDAD6644-1810-11D1-86AE-0000F8751720} IGroupLayer
            //{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF} IRasterLayer
            UID uid = new UIDClass();
            uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";
            IEnumLayer layers = mHookHelper.FocusMap.get_Layers(uid, true);
            return layers;
        }
        //获取图层并设置到ComboBox中
        public void getLayer(ComboBox mComBox)
        {
            if (mHookHelper == null || mHookHelper.Hook == null || mHookHelper.FocusMap.LayerCount == 0)
            {
                return;
            }
            IEnumLayer layers = GetLayers();
            layers.Reset();
            ILayer layer = null;
            while ((layer = layers.Next()) != null)
            {
                mComBox.Items.Add(layer.Name);
            }
            if (mComBox.Items.Count > 0)
            {
                mComBox.SelectedIndex = 0;
            }
        }

        //获取得到图层集，如果获取得到返回true
        public bool getFeatureLayer(string[] LayerName)
        {
            if (LayerName.GetLength(0) >= 0)
            {
                mILayer = new ILayer[LayerName.GetLength(0)];
                IEnumLayer layers = GetLayers();
                layers.Reset();
                ILayer layer = null;
                int i = 0;
                int j = 0;
                while (i < LayerName.GetLength(0))
                {
                    while ((layer = layers.Next()) != null)
                    {
                        if (LayerName[i] == layer.Name)
                        {
                            mILayer[j] = layer as IFeatureLayer;
                            j++;
                        }

                    }
                    layers.Reset();
                    i++;
                }
                return true;
            }
            else
            {
                return false;
            }

        }
        public void otherFeaturePath()
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.CheckPathExists = true;
            saveDlg.Filter = "Shapefile (*.shp)|*.shp";
            saveDlg.OverwritePrompt = true;
            saveDlg.Title = "Output Layer";
            saveDlg.RestoreDirectory = true;
            saveDlg.FileName = (string)strInputPath + "_Intersect.shp";
            DialogResult dt = saveDlg.ShowDialog();
            if (dt == DialogResult.OK)
            {
                strOutputPath = saveDlg.FileName;
            }
        }
        //属性是否完全
        public bool IsProperty()
        {
            string mMessageString="";
            bool isTrue = true;
            if (mOverLayType == null)
            {
                isTrue = false;
                mMessageString += "缺少操作类型参数：mOverLayType"+"/r/n";
            }

            if (!isTrue)
            {
                MessageBox.Show(mMessageString);
                return isTrue;
            }
            return true;
        }
        /// <summary>
        /// 输入参数并执行叠置分析
        /// </summary>
        /// <param name="myLayers">输入要素图层组</param>
        public void mOverLay()
        {
            ILayer[] myLayers = mILayer;
            //判断要素图层组是否合符要求
            if (myLayers.GetLength(0) < 2) return;
            int layerCounts = this.LayerCount;

            //实例空间分析方法
            ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();

            //允许覆盖已有结果
            gp.OverwriteOutput = true;

            //判断叠置分析执行类型，参考overLayType
            if (mOverLayType == overLayType.Intersect)
            {

               
                //实例Intersect方法
                Intersect mIntersect = new Intersect();
                object[] myObject = new object[myLayers.GetLength(0)];
                myObject = myLayers;

                //Intersect中的内容列表
                IGpValueTableObject mObject = new GpValueTableObjectClass();
                mObject.SetColumns(myLayers.GetLength(0));
                for (int y = 0; y < myLayers.GetLength(0); y++)
                {
                    object test = myObject[y];
                    mObject.AddRow(ref test);
                }

                //Intersect中的Input Features
                mIntersect.in_features = mObject;
                if (strOutputPath == "")
                {
                    //Intersect中的OutPut Features Class
                    strOutputPath = System.IO.Path.GetTempPath() + strInputPath + "_Intersect.shp";
                }
                mIntersect.out_feature_class = strOutputPath;
               
                //执行结果消息
                IGeoProcessorResult result = gp.Execute(mIntersect, null) as IGeoProcessorResult;
                if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
                {
                    MessageBox.Show("叠置求交失败");
                }
                else
                {
                    MessageBox.Show("叠置求交成功");
                    //获取输出shp所在的文件夹
                    string strFilePath = System.IO.Path.GetDirectoryName(strOutputPath);
                    //获取输出shp的文件名
                    string streFileName = System.IO.Path.GetFileName(strOutputPath);
                    //添加叠置shp文件
                    //this.mAxMapControls.AddShapeFile(strFilePath, streFileName);
                }
            }
            if (mOverLayType == overLayType.Union)
            {
                //实例Union方法
                Union mIntersect = new Union();
                object[] myObject = new object[myLayers.GetLength(0)];
                myObject = myLayers;

                //Intersect中的内容列表
                IGpValueTableObject mObject = new GpValueTableObjectClass();
                mObject.SetColumns(myLayers.GetLength(0));
                for (int y = 0; y < myLayers.GetLength(0); y++)
                {
                    object test = myObject[y];
                    mObject.AddRow(ref test);
                }

                //Intersect中的Input Features
                mIntersect.in_features = mObject;
                if (strOutputPath=="")
                //Intersect中的OutPut Features Class
                strOutputPath = System.IO.Path.GetTempPath() + strInputPath + "_Union.shp";
                mIntersect.out_feature_class = strOutputPath;

                //执行结果消息
                IGeoProcessorResult result = gp.Execute(mIntersect, null) as IGeoProcessorResult;
                if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
                {
                    MessageBox.Show("叠置求并失败");
                }
                else
                {
                    MessageBox.Show("叠置求并成功");
                    //获取输出shp所在的文件夹
                    string strFilePath = System.IO.Path.GetDirectoryName(strOutputPath);
                    //获取输出shp的文件名
                    string streFileName = System.IO.Path.GetFileName(strOutputPath);
                    //添加缓冲区shp文件
                    this.mAxMapControls.AddShapeFile(strFilePath, streFileName);
                }
            }
            if (mOverLayType == overLayType.Identity)
            {
                //实例Identity方法
                Identity mIntersect = new Identity();
                object[] myObject = new object[myLayers.GetLength(0)];
                myObject = myLayers;

                //Intersect中的内容列表
                IGpValueTableObject mObject = new GpValueTableObjectClass();
                mObject.SetColumns(myLayers.GetLength(0));
                for (int y = 0; y < myLayers.GetLength(0); y++)
                {
                    object test = myObject[y];
                    mObject.AddRow(ref test);
                }

                //Intersect中的Input Features
                mIntersect.in_features = mObject;

                //Intersect中的OutPut Features Class
                strOutputPath = System.IO.Path.GetTempPath() + strInputPath + "_Union.shp";
                mIntersect.out_feature_class = strOutputPath;

                //执行结果消息
                IGeoProcessorResult result = gp.Execute(mIntersect, null) as IGeoProcessorResult;
                if (result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
                {
                    MessageBox.Show("叠置表示失败");
                }
                else
                {
                    MessageBox.Show("叠置表示成功");
                    //获取输出shp所在的文件夹
                    string strFilePath = System.IO.Path.GetDirectoryName(strOutputPath);
                    //获取输出shp的文件名
                    string streFileName = System.IO.Path.GetFileName(strOutputPath);
                    //添加缓冲区shp文件
                    this.mAxMapControls.AddShapeFile(strFilePath, streFileName);
                }
 
            }
        }
        
    }
}
